package com.zhuoyue.common.dto;

import com.zhuoyue.common.constant.ResultCode;
import lombok.NoArgsConstructor;

import java.util.HashMap;
import java.util.Map;

/**
 * 单个结果封装
 * @author Administrator
 */
@NoArgsConstructor
public class ResultDTO<T> extends BaseResultDTO {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7724400142973976404L;

	protected T module;

	/**
	 * 保存详细的校验错误信息
	 */
	protected Map<String, String> checkErrorInfo = new HashMap<String, String>(3);


	public ResultDTO(T module){
		this.module = module;
	}

	public T getModule() {
		return module;
	}

	public void setModule(T module) {
		this.module = module;
	}

	public void addCheckErrorInfo(String code, String message) {
		checkErrorInfo.put(code, message);
	}

	public void addCheckErrorInfos(Map<String, String> infos) {
		checkErrorInfo.putAll(infos);
	}

	public Map<String, String> getCheckErrorInfo() {
		return checkErrorInfo;
	}

	public static <T> ResultDTO<T> getResult() {
		return new ResultDTO<T>();
	}
	
	public boolean isExist() {
		return null != module;
	}


	/**
	 * 参数错误
	 * @param msg
	 * @return
	 */
	public ResultDTO<T> toParamError(String msg) {
	    this.success=false;
	    this.setErrorDetail(msg);
	    this.setResultCode(ResultCode.PARAM_ERROR);
	    return this;
	}

	/**
	 * 系统错误
	 * @param msg
	 * @return
	 */
	public ResultDTO<T> toSystemError(String msg) {
	    this.success=false;
	    this.setErrorDetail(msg);
	    this.setResultCode(ResultCode.SYS_ERROR);
	    return this;
	}

	public ResultDTO<T> toNoDataError(String msg) {
		this.success=false;
		this.setErrorDetail(msg);
		this.setResultCode(ResultCode.NO_DATA);
		return this;
	}

	/**
	 * 查询错误
	 * @param msg
	 * @return
	 */
	public ResultDTO<T> toSelectFailed(String msg) {
		this.success=false;
		this.setErrorDetail(msg);
		this.setResultCode(ResultCode.NO_DATA);
		return this;
	}

	/**
	 * 查询成功
	 * @param model
	 * @return
	 */
	public ResultDTO<T> toSuccess(T model) {
		this.success=true;
		this.setErrorDetail("获取信息成功");
		this.setResultCode(ResultCode.SUCCESS);
		this.setModule(model);
		return this;
	}

	@Override
	public String toString() {
		return "ResultDTO{" +
				"module=" + module +
				", checkErrorInfo=" + checkErrorInfo +
				'}';
	}
}
